package cn.melon.window;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import cn.melon.helper.BufferedImageHelper;
import cn.melon.view.JCircleLabel;
import cn.melon.view.JImgPanel;
import cn.melon.view.control.AccountInputTip;
import cn.melon.view.control.CloseWindowEventListener;
import cn.melon.view.control.LoginRequest;
import cn.melon.view.control.MinWindowEventListener;
import cn.melon.view.control.MouseDraggedAdapter;
import cn.melon.view.control.MouseLocationAdapter;
import cn.melon.view.control.PassInputTip;

/**
 * @ClassName: LoginWindow
 * @author Hzy
 * 登录界面
 */
public class LoginWindow extends JFrame {
    // final private int WINDOW_WIDTH = 370;
    // final private int WINDOW_HEIGHT = 526;
    final private int WINDOW_WIDTH = 384;
    final private int WINDOW_HEIGHT = 540;
    final private int HEAD_RADIUS = 35;
    final private int LOGBTN_WIDTH = 163;
    final private int LOGBTN_HEIGHT = 37;

    private JCircleLabel head; // 头像控件
    private JTextField accountInput;// 账户输入
    private JPasswordField passInput;// 密码输入
    private JCheckBox checkStudent;// 登录为学生账户选项
    private JCheckBox checkAdmin;// 登录为管理员账户选项
    private JButton logButton;// 登录按钮

    public LoginWindow() {
        this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        this.setUndecorated(true); // 无边框，无标题栏
        this.setResizable(false);

        // 文件路径变量
        String iconPath = ""; // 程序小图标路径
        String headPath = ""; // 头像文件路径
        String bacPath = ""; // 背景图像路径
        String minPath = ""; // 最小化按钮图像路径
        String closePath = ""; // 关闭按钮图像路径
        String logBtnPath = "";// 登陆按钮图像路径
        String melonPath = "";
        // 获取文件路径
        try {
            ClassLoader classLoader = getClass().getClassLoader();
            String pathTmp; // 路径临时变量
//            pathTmp = classLoader.getResource("img/melon-icon-20.png").getFile();
//            iconPath = java.net.URLDecoder.decode(pathTmp, "utf-8");
//            pathTmp = classLoader.getResource("img/head.jpg").getFile();
//            headPath = java.net.URLDecoder.decode(pathTmp, "utf-8");
//            pathTmp = classLoader.getResource("img/bac-img-v-s.png").getFile();
//            bacPath = java.net.URLDecoder.decode(pathTmp, "utf-8");
            pathTmp = classLoader.getResource("img/min.png").getFile();
            minPath = java.net.URLDecoder.decode(pathTmp, "utf-8");
            pathTmp = classLoader.getResource("img/close.png").getFile();
            closePath = java.net.URLDecoder.decode(pathTmp, "utf-8");
            pathTmp = classLoader.getResource("img/logButton.png").getFile();
            logBtnPath = java.net.URLDecoder.decode(pathTmp, "utf-8");

            iconPath = "/img/melon-icon-20.png";
            headPath = "/img/head.jpg";
            bacPath = "/img/bac-img-v-s.png";
            minPath = "/img/min.png";
            closePath = "/img/close.png";
            logBtnPath = "/img/logButton.png";
            
        } catch (Exception e) {
            e.printStackTrace();
        }

        JImgPanel logWindow = new JImgPanel(bacPath, WINDOW_WIDTH, WINDOW_HEIGHT);
        this.setBackground(new Color(0, 0, 0, 0));
        logWindow.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        // 标题栏设置
//        ImageIO.read(this.getClass().getResource(imgPath));
//        JLabel softwareIcon = new JLabel(new ImageIcon(iconPath));
//
        JLabel softwareIcon = null;
        softwareIcon = 
                new JLabel(new ImageIcon(BufferedImageHelper.getSource(this.getClass(), iconPath)));
//
//        JButton minIcon = new JButton(new ImageIcon(minPath));
        JButton minIcon = new JButton(new ImageIcon(BufferedImageHelper.getSource(this.getClass(), minPath)));
        minIcon.setPreferredSize(new Dimension(28, 28));
        minIcon.setContentAreaFilled(false);
        minIcon.setBorderPainted(false);
        minIcon.setFocusPainted(false);
        minIcon.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
//        JButton closeIcon = new JButton(new ImageIcon(closePath));
        JButton closeIcon = 
                new JButton(new ImageIcon(BufferedImageHelper.getSource(this.getClass(), closePath)));
        closeIcon.setPreferredSize(new Dimension(28, 28));
        closeIcon.setContentAreaFilled(false);
        closeIcon.setBorderPainted(false);
        closeIcon.setFocusPainted(false);
        closeIcon.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        JLabel softwareName = new JLabel("瓜田v1.0");
        softwareName.setForeground(Color.white);
        softwareName.setFont(new java.awt.Font("微软雅黑", 0, 13));
        // 头像
        head = new JCircleLabel(HEAD_RADIUS, headPath);
        head.setPreferredSize(new Dimension(2 * HEAD_RADIUS, 2 * HEAD_RADIUS));
        // 账户密码输入
        accountInput = new JTextField();
        accountInput.setForeground(Color.gray);
        String accountTip = new String("请输入账户");
        accountInput.setText(accountTip);
        accountInput.setPreferredSize(new Dimension(195, 25));
        accountInput.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        passInput = new JPasswordField();
        passInput.setForeground(Color.gray);
        passInput.setEchoChar('\0');
        String passTip = new String("请输入密码");
        passInput.setText(passTip);
        passInput.setBorder(BorderFactory.createLineBorder(Color.black, 1));
        passInput.setPreferredSize(new Dimension(195, 25));
        // 登录账户类型选择
        checkAdmin = new JCheckBox("管理员");
        checkAdmin.setOpaque(false);
        checkAdmin.setForeground(Color.white);
        checkAdmin.setFont(new java.awt.Font("微软雅黑", 0, 13));
        checkAdmin.setFocusPainted(false);
        checkStudent = new JCheckBox("学生");
        checkStudent.setOpaque(false);
        checkStudent.setForeground(Color.white);
        checkStudent.setFont(new java.awt.Font("微软雅黑", 0, 13));
        checkStudent.setFocusPainted(false);
        checkStudent.setSelected(true);
        ButtonGroup accountType = new ButtonGroup();
        accountType.add(checkAdmin);
        accountType.add(checkStudent);
        // 登录按钮
//        logButton = new JButton(new ImageIcon(logBtnPath));
        logButton = new JButton(new ImageIcon(BufferedImageHelper.getSource(this.getClass(), logBtnPath)));
        logButton.setContentAreaFilled(false);
        logButton.setPreferredSize(new Dimension(LOGBTN_WIDTH, LOGBTN_HEIGHT));
        logButton.setBorder(null);
        logButton.setFocusPainted(false);

        // 布局
        logWindow.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        // 第一行高度权重
        gbc.weighty = 0.169;

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.1;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(15, 12, 0, 0);
        gbc.ipadx = 0;
        gbc.ipady = 0;
        logWindow.add(softwareIcon, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.1;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(16, -8, 0, 0);
        gbc.ipadx = 0;
        gbc.ipady = 0;
        logWindow.add(softwareName, gbc);

        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.anchor = GridBagConstraints.NORTHEAST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(10, 0, 0, 0);
        gbc.ipadx = 0;
        gbc.ipady = 0;
        logWindow.add(minIcon, gbc);

        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.gridheight = 1;
        gbc.weightx = 0.1;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(10, 0, 0, -15);
        gbc.ipadx = 0;
        gbc.ipady = 0;
        logWindow.add(closeIcon, gbc);

        // 第二行高度权重
        gbc.weighty = 1;

        gbc.gridx = GridBagConstraints.REMAINDER;
        gbc.gridy = 1;
        gbc.weightx = 1;
        gbc.anchor = GridBagConstraints.CENTER;
        logWindow.add(head, gbc);

        // 第三行高度权重
        gbc.weighty = 0.303;

        gbc.gridx = GridBagConstraints.REMAINDER;
        gbc.gridy = 2;
        gbc.weightx = 1;
        gbc.anchor = GridBagConstraints.NORTH;
        logWindow.add(accountInput, gbc);

        // 第四行高度权重
        gbc.weighty = 0.303;

        gbc.gridx = GridBagConstraints.REMAINDER;
        gbc.gridy = 3;
        gbc.weightx = 1;
        gbc.anchor = GridBagConstraints.NORTH;
        logWindow.add(passInput, gbc);

        // 第五行高度权重
        gbc.weighty = 0.080;

        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.weightx = 1;
        // gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(0, -120, 0, 0);
        logWindow.add(checkAdmin, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.weightx = 1;
        gbc.insets = new Insets(0, 120, 0, 0);
        // gbc.anchor = GridBagConstraints.WEST;
        logWindow.add(checkStudent, gbc);

        // 第六行
        gbc.weighty = 0.746;

        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.weightx = 1;
        gbc.insets = new Insets(0, 0, 0, 0);
        gbc.anchor = GridBagConstraints.CENTER;
        logWindow.add(logButton, gbc);

        this.getContentPane().add(logWindow);
        // 使窗体打开时自动居中
        this.setLocationRelativeTo(null);
        // 为窗口添加拖动
        MouseLocationAdapter m = new MouseLocationAdapter();
        this.addMouseListener(m);
        this.addMouseMotionListener(new MouseDraggedAdapter(this, m.getOriginPoint()));

        // 添加事件
        minIcon.addActionListener(new MinWindowEventListener(this));
        closeIcon.addActionListener(new CloseWindowEventListener(this));
        accountInput.addFocusListener(new AccountInputTip(accountInput, accountTip));
        passInput.addFocusListener(new PassInputTip(passInput, passTip));
        logButton.addActionListener(new LoginRequest(accountInput,passInput, checkStudent, this));
    }
}
